import XLSX from 'xlsx'
/**
 * 读取base64编码文件
 * @param file
 */
export const getBase64 = function (file, callback) {
    let reader = new FileReader()
    reader.onloadend = function () {
        callback(reader.result)
    }
    if(file) {
        reader.readAsDataURL(file)
    }
}

///excel导入功能使用：
// 1、首先要安装SheetJs, npm install xlsx --save;
// 2、在需要的文件中引入importExcel.js文件
// 3、参数说明：file是指需要导入的excel文件（会有弹窗选择即可）；colnumData是指对应数据库表的列名与注释组成的集合
// 4、excel的列名称要与数据库表中的注释保持一致
export function importExcel(file,colnumData){
    const types = file.name.split('.').pop();
    let dataList = [];
    const fileType = ['xlsx', 'xlc', 'xlm', 'xls', 'xlt', 'xlw', 'csv'].some(item => item === types);
    if (!fileType) {
        this.$message('格式错误！请重新选择')
        return
    }
    return new Promise(function(resolve,reject){
        file2Xce(file).then(tabJson => {
            if (tabJson && tabJson.length > 0) {
                let columnMapping = colnumData;
                const colnumName = Object.keys(columnMapping);
                let dataArr = tabJson[0].sheet;
                dataArr.forEach(item=>{
                    let itemMap = new Map();
                    for (let key in item) {
                        if(colnumName.includes(key)){
                            itemMap.set(columnMapping[key],item[key])
                        }else{
                            reject(key+"  列名不存在，请核对！");
                            return;
                        }
                    }
                    dataList.push(itemMap);
                });
                resolve(map2json(dataList));
            }else{
                reject("EXCEL文件没有数据！")
            }
        })
    })
}

function file2Xce(file) {
    return new Promise(function(resolve, reject) {
        const reader = new FileReader()
        reader.onload = function(e) {
            const data = e.target.result
            this.wb = XLSX.read(data, {
                type: 'binary'
            })
            const result = []
            this.wb.SheetNames.forEach((sheetName) => {
                result.push({
                    sheetName: sheetName,
                    sheet: XLSX.utils.sheet_to_json(this.wb.Sheets[sheetName])
                })
            })
            resolve(result)
        }
        reader.readAsBinaryString(file.raw)
    })
}

function map2json (list) {
    let datalist = [];
    list.forEach(element => {
        let json = {};
        for(let [k,v] of element){
            json[k]=v;
        }
        datalist.push(json);
    });
    return datalist;
}

/**
 * 正则判断
 * @param type
 * @param s
 * @returns {*}
 */
export const regTest = function (type, s) {
    const regs = {
        "name" : /^[\u4E00-\u9FA5]{2,4}$/,
        "date" : /^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$/,
        "phone" : /^[1][3,4,5,7,8,9][0-9]{9}$/,
        "email" : /^([A-Za-z0-9_\-\\.])+\\@([A-Za-z0-9_\-\\.])+\.([A-Za-z]{2,4})$/,
        "idCard" : /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
        "money": /^([1-9][0-9]*)+(.[0-9]{1,2})?$/,
        "id": /^[1-9]\d*$/
    }
    return regs[type].test(s)
}

/**
 * 对象克隆
 * @param obj
 * @returns {any}
 */
export const objectClone = function (obj) {
    return JSON.parse(JSON.stringify(obj))
}